<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;

class User extends Controller
{
    //查询
    public function select()
    {
    //原声sql
        $list = Db::query('SELECT * FROM user');
        $list = Db::query('SELECT * FROM user WHERE id=?',[1]);
        //Db类
        $list = Db::table('user')->select();
        //加前缀
//        $lsit = Db::name('user')->order(['id'=>'asc'])->select();

    //助手函数  //加前缀
//        $list = db('lover')->order(['id' => 'asc'])->select();
        dump($list);
    }

//    添加
    public function insert()
    {
        //原声sql
//        $result = Db::execute('INSERT INTO `user` (`name`,`age`) VALUES ("kk1", "82")');
//        $result = Db::execute('INSERT INTO `user` (`name`,`age`) VALUES (:name, :age)', ['name'=>'kk3', 'age'=>'85']);
        $data = [
            'name' => 'kk9',
            'age' => '49'
        ];
        // DB类
//        $result = Db::table('user')->insertGetId($data);
        $result = Db::table('user')->data($data)->insert();
        // 助手函数
//        $result = db('user')->insert($data);
        dump($result);
    }

    //删除
    public function delete()
    {
        //原声sql
//        $data = Db::execute("DELETE FROM `user` WHERE name='kk8'");
        //使用DB类
//        $data = Db::table('user')->delete(70);
        $data = Db::table('user')->where('name','kk3')->delete();

        // 助手函数
//        $result = db()->table('user')->delete(68);
        dump($data);
    }
    
    //更改
    public function update()
    {
        //原声sql
//        $data = Db::execute("UPDATE `user` SET age='28' WHERE id='65'");

        //使用DB类
//        $data = Db::table('user')->where('id',65)->update(['age'=>18]);

        // 助手函数
//        $result = db()->table('user')->where('id',1)->update(['age'=>18]);
        dump($data);
    }
}
